package cn.bby.tools.dialog.common;

import cn.bby.tools.utils.ClipboardUtil;
import cn.bby.tools.utils.MessageUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * 结果显示对话框
 * 用于显示多行字符串结果，用户可点击确认复制到剪贴板
 *
 * @author ben
 * @version 1.0
 */
public class BaseResultDisplayDialog extends BaseValidationDialog {

    protected final String defResultText;
    protected final JTextArea resultTextArea = new JTextArea(15, 60);
    protected final Project project;
    public BaseResultDisplayDialog(@Nullable Project project, String title) {
        super(project);
        defResultText = "";
        this.project = project;
        setTitle(title);
        //自动换行
        resultTextArea.setLineWrap(true);
    }

    /**
     * 构造函数
     *
     * @param project    项目实例
     * @param title      对话框标题
     * @param defResultText 结果文本
     */
    public BaseResultDisplayDialog(@Nullable Project project, String title, String defResultText) {
        super(project);
        this.defResultText = defResultText;
        this.project = project;
        init();
        setTitle(title);
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = JBUI.insets(5);
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;

        // 设置结果显示区域
        resultTextArea.setText(defResultText);
        resultTextArea.setEditable(false);
        resultTextArea.setBorder(BorderFactory.createLoweredBevelBorder());
        JScrollPane scrollPane = new JScrollPane(resultTextArea);

        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(scrollPane, gbc);

        return panel;
    }

    public void setResultTextEditable(boolean editable) {
        resultTextArea.setEditable(editable);
    }

    @Override
    protected @NotNull Action @NotNull [] createActions() {
        // 创建自定义按钮：复制并关闭 和 关闭
        DialogWrapperAction copyAndCloseAction = new DialogWrapperAction(MessageUtil.getMessage("message.CopyAndClose")) {
            @Override
            protected void doAction(ActionEvent e) {
                doCopyAndCloseAction();
            }
        };

        DialogWrapperAction closeAction = new DialogWrapperAction(MessageUtil.getMessage("message.Close")) {
            @Override
            protected void doAction(ActionEvent e) {
                doCancelAction();
            }
        };

        return new Action[]{copyAndCloseAction, closeAction};
    }

    /**
     * 执行复制并关闭操作
     */
    protected void doCopyAndCloseAction() {
        String result = resultTextArea.getText();
        if (result != null && !result.isEmpty()) {
            ClipboardUtil.setTextToClipboard(result);
        }
        close(OK_EXIT_CODE);
    }

}